package com.ziq.zcamp.place

import android.content.Context
import android.graphics.Color
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter
import com.ziq.base.extension.click
import com.ziq.zcamp.R
import com.ziq.zcamp.api.respond.camp.CampConditionTree
import com.ziq.zcamp.bean.place.CampConditionCategory
import com.ziq.zcamp.databinding.ItemFilterAreaContentAreaBinding
import com.ziq.zcamp.databinding.ItemFilterAreaContentAreaHeaderBinding
import com.ziq.zcamp.view.layoutmanager.FilterAreaCategoryContentGridLayoutManager

class FilterConditionCategoryContentAdapter(context: Context?) : GroupedRecyclerViewAdapter(context) , FilterAreaCategoryContentGridLayoutManager.ChildSpanSize{

    var dataList: List<CampConditionTree>? = null
    var sourcePosition: Int = 0 //控制UI 颜色
    fun setData(list: List<CampConditionTree>?, sourcePosition:Int = 0){
        this.dataList = list
        this.sourcePosition = sourcePosition
        notifyDataSetChanged()
    }


    private var selectedList : MutableMap<Int, MutableList<Int>>? = null
    fun updateSelectedCondition(selectedConditionList : MutableMap<Int, MutableList<Int>>?){
        this.selectedList = selectedConditionList
        notifyDataSetChanged()
    }

    var onSelectListener: ((categoryId:Int?, categoryType: Int?, conditionId:Int?, conditionName:String?)->Unit)? = null
    var onUnSelectListener: ((categoryId:Int?, categoryType: Int?, conditionId:Int?, conditionName:String?)->Unit)? = null

    fun getFirstVisibleCategory(firstPosition:Int) : Int{
        val groupPosition = getGroupPositionForPosition(firstPosition)
        if(groupPosition >= 0){
            return dataList?.get(groupPosition)?.id?:-1
        }
        return -1
    }

    fun getGroupPosition(category : Int) : Int{
        if(dataList != null){
            val length = dataList!!.size
            for (i in 0 until length){
                if(dataList?.get(i)?.id == category){
                    return countGroupRangeItem(0, i)
                }
            }
        }
        return 0
    }


    override fun getChildSpanSize(groupPosition: Int, childPosition: Int): Int {
        return 1
    }

    override fun getGroupCount(): Int {
        return dataList?.size?:0
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return dataList?.get(groupPosition)?.children?.size?:0
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return true
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return false
    }

    override fun getHeaderLayout(viewType: Int): Int {
        return R.layout.item_filter_area_content_area_header
    }

    override fun getChildLayout(viewType: Int): Int {
        return R.layout.item_filter_area_content_area
    }

    override fun getFooterLayout(viewType: Int): Int {
        return 0
    }

    override fun onBindHeaderViewHolder(holder: com.donkingliang.groupedadapter.holder.BaseViewHolder?, groupPosition: Int) {
        holder?.let {
            val item = dataList?.get(groupPosition)
            val binding = ItemFilterAreaContentAreaHeaderBinding.bind(holder.itemView)
            binding.title.text = item?.name
            when(sourcePosition%3){
                0 ->{
                    binding.vLine.setBackgroundColor(Color.parseColor("#BA9A80"))
                }
                1 ->{
                    binding.vLine.setBackgroundColor(Color.parseColor("#A09F88"))
                }
                2 ->{
                    binding.vLine.setBackgroundColor(Color.parseColor("#9E503F"))
                }
            }

        }
    }


    override fun onBindChildViewHolder(
        holder: com.donkingliang.groupedadapter.holder.BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {
        holder?.let {
            val categoryItem = dataList?.get(groupPosition)
            val conditionItem = categoryItem?.children?.get(childPosition)
            val binding = ItemFilterAreaContentAreaBinding.bind(holder.itemView)
            binding.name.text = conditionItem?.name
            var selected = false
            if(selectedList?.containsKey(categoryItem?.id?:-1) == true){
                if(selectedList?.get(categoryItem?.id?:-1)?.contains(conditionItem?.id?:-1) == true){
                    selected = true
                }
            }
            binding.name.isSelected = selected
            holder.itemView.click {
                if(selected){
                    onUnSelectListener?.invoke(categoryItem?.id, categoryItem?.multi_select, conditionItem?.id, conditionItem?.name)
                } else {
                    onSelectListener?.invoke(categoryItem?.id, categoryItem?.multi_select, conditionItem?.id, conditionItem?.name)
                }
            }
        }
    }

    override fun onBindFooterViewHolder(holder: com.donkingliang.groupedadapter.holder.BaseViewHolder?, groupPosition: Int) {
    }

}